#ifndef BATTLEENGINE_H
#define BATTLEENGINE_H

#include <QQueue>
#include <QTimer>
#include <QString>
#include "Unit.h"
#include "Building.h"
#include <QPoint>
#include <qmath.h>
#include "Command.h"

class BattleEngine : public QObject{

    private: Q_OBJECT

    public:
        explicit BattleEngine(Ui::MainClass* u);
        ~BattleEngine();

        void connectToInterface(Command* c);

        bool anyAttackableEnemyUnitsInRange(Unit* base);
        QList <Unit*>* getAttackableEnemyUnits(Unit* base);
        Unit* getNearestEnemyUnitFromSet(Unit* base, QList <Unit*>* set);
        QPoint* vectorXY(int magnitude, int angle, int baseX, int baseY);

    private:
        bool unitCanAttackUnit(Unit* base, Unit* target);
        void placeBuildingAtSlot();
        void gameStart();
        void buildSpriteBase();

    private slots:
        void economyRound();
        void technologyRound();
        void cleanUp(); //removes dead units from battle field
        void engageSelectButton();
        void disengageSelectButton();
        void animateHighlighted();

    public slots:
        void spawnInfLv1();

        void spawnUnit(QString defType, QString atkType, int level, bool isRed);
        void spawnBuilding(QString type, int slot, int isRed);
        void deselectAllRedBuildings();
        void deselectAllBlueBuildings();
        void startCombatSession();
        void endCombatSession();

        void commandInterface();

    public:
        QPushButton* highlighted;
        int animationIndex;
        bool cleanSession; //true if a clean session if being done
        QList <Unit*>* redUnits;
        QList <Unit*>* blueUnits;
        QList <QIcon*>* iconAnim;

        QTimer* selectButtonAnimator;
        QTimer* cleaner; //timer for checking on dead units in the battlefield
        QTimer* resourceRound; //timer for resource rounds

        int ecoProd[6];
        int techProd[3];

        bool playerIsRed;

        //Red Player Stats
        bool redIsAI;
        int redMoney;
        int redEconomy;
        int redTech;
        int redTechProd;

        Building* redBlg[8];

        //Blue Player Stats
        bool blueIsAI;
        int blueMoney;
        int blueEconomy;
        int blueTech;
        int blueTechProd;

        Building* blueBlg[8];

        Ui::MainClass *ui;

        QPixmap* blgSpriteBase[2][6][6][2];
        //red = 0, blue = 1
        //cc = 0, brks = 1, wrfctry = 2, airfld = 3, ecoC = 4, techC = 5
        //lv1 = 0, lv2 = 1, lv3 = 2, lv4 = 3, lv5 = 4, lv6 = 5
        //off = 0, on = 1

        QList <QPixmap*>* unitSpriteBase[2][3][3][3][3];
        //unitSpriteBase[blue/red][air/inf/veh][aa/ai/at][lv1/lv2/lv3][wlk/atk/std]
        //red = 0, blue = 1
        //air = 0, inf = 1, veh = 2
        //aa = 0, ai = 1, at = 2
        //lv1 = 0, lv2 = 1, lv3 = 2
        //wlk = 0, atk = 1, std = 2

};

#endif // BATTLEENGINE_H
